#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：back 
@File    ：task.py
@IDE     ：PyCharm 
@Author  ：七分是你
@Date    ：2024/6/29 上午10:23 
@explain : 
"""
from datetime import datetime

from app import dao


class Task:
    @classmethod
    def do_task(cls, uid, utype, alias):
        print("do_task===", uid)
        alias_arr = alias if isinstance(alias, list) else [alias]
        today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
        timestamp_today = int(today.timestamp())
        where = {'utype': utype, 'alias': alias_arr}
        task_data = dao.TaskQueryDao.select(**where)
        sqldata_arr = []
        for task_info in task_data:
            map = {}
            if task_info.daily == 1:
                map['uid'] = uid
                map['alias'] = task_info.alias
                map['addtime'] = timestamp_today
                already_done_total = dao.TaskQueryDao.get_count(mode_name='Record', **map)
                if already_done_total >= task_info.max_perday:
                    continue
            elif task_info.daily == 0:
                map['uid'] = uid
                map['alias'] = task_info.alias
                already_done_total = dao.TaskQueryDao.get_count(mode_name='Record', **map)
                if already_done_total > 0:
                    continue
            data = {}
            data['uid'] = uid
            data['alias'] = task_info.alias
            data['addtime'] = timestamp_today
            data['points'] = task_info.points
            data['note'] = task_info.name
            sqldata_arr.append(data)
        if len(sqldata_arr) > 0:
            print("sqldata_arr===", sqldata_arr)
            for value in sqldata_arr:
                if value['points'] > 0:
                    dao.MemberOperateDao.set_member_points(
                        {'uid': uid, 'points': value['points'], 'note': value['note']})
            dao.TaskOperateDao.save_task_record(sqldata_arr)
